const {
    Mysql,
    validateParams
} = require('../tools');
module.exports = async ctx => {
    let querys = 'vid,title,coverImg,isNew,isVip,views,type,sort'
    let {
        from,
        query,
        pageNum,
        pageSize,
        sort,
        type
    } = ctx.request.body;
    let isTrue = validateParams({
        from
    })
    if (!isTrue) {
        ctx.state.data = {
            err: 'خاتالىق كۆرۈلدى',
            code: '002'
        }
        return;
    }
    // 模糊搜索
    if (from == 'search') {
        let sql = `SELECT ${querys} FROM video WHERE title like '%${query}%' limit 20`;
        let list = await Mysql(sql);
        ctx.state.data = {
            data: {
                list
            },
            code: '001'
        }
    }
    // 如果是根据类型搜索
    else if (from == 'sort') {
        let sql = `SELECT ${querys} from (SELECT * from video WHERE sort='${sort}' ORDER BY create_time DESC)video LIMIT ${(pageNum-1)*pageSize},${pageSize}`;
        try {
            let list = await Mysql(sql);
            ctx.state.data = {
                data: {
                    list
                },
                code: '001'
            }
        } catch (error) {
            ctx.state.data = {
                code: '002',
                err: 'مەغلۇپ بولدى'
            }
        }
    }
    // 如果是收费电影
    else if (from == 'COST') {
        let sql = `SELECT ${querys} from (SELECT * from video WHERE video.isFree=1 OR video.price>0 ORDER BY create_time DESC)video LIMIT ${(pageNum-1)*pageSize},${pageSize}`;
        try {
            let list = await Mysql(sql);
            // let listStr = Buffer.from(JSON.stringify(list)).toString('base64').replace(/A/g, '&').replace(/B/g, '%'); //第一次转base64
            ctx.state.data = {
                data: {
                    list
                },
                code: '001'
            }
        } catch (error) {
            ctx.state.data = {
                code: '002',
                err: 'مەغلۇپ بولدى'
            }
        }
    }
    // 如果是免费电影
    else if (from == 'FREE') {
        let sql = `SELECT ${querys} from (SELECT * from video WHERE video.isVip !=1 ORDER BY create_time DESC)video LIMIT ${(pageNum-1)*pageSize},${pageSize}`;
        try {
            let list = await Mysql(sql);
            // let listStr = Buffer.from(JSON.stringify(list)).toString('base64').replace(/A/g, '&').replace(/B/g, '%'); //第一次转base64
            ctx.state.data = {
                data: {
                    list
                },
                code: '001'
            }
        } catch (error) {
            ctx.state.data = {
                code: '002',
                err: 'مەغلۇپ بولدى'
            }
        }
    }
    // 获取随机推荐电影
    else if (from == 'recommend') {
        let sql = `SELECT ${querys} FROM video WHERE sort='${sort}' ORDER BY RAND() LIMIT 6`;
        try {
            let list = await Mysql(sql);
            ctx.state.data = {
                data: {
                    list
                },
                code: '001'
            }
        } catch (error) {
            ctx.state.data = {
                code: '002',
                err: 'مەغلۇپ بولدى'
            }
        }
    }
    // 获取会员专属电影
    else if (from == 'VIP') {
        let sql = `SELECT ${querys} from (SELECT * from video WHERE isVip=1 ORDER BY create_time DESC)video LIMIT ${(pageNum-1)*pageSize},${pageSize}`;
        try {
            let list = await Mysql(sql);
            ctx.state.data = {
                data: {
                    list,
                },
                code: '001'
                // total: total[0].total,
            }
        } catch (error) {
            ctx.state.data = {
                code: '002',
                err: 'مەغلۇپ بولدى'
            }
        }
    } 
    // 新电影
    else if (from == 'new') {
        let sql = `SELECT ${querys} FROM video ORDER BY create_time DESC limit ${(pageNum-1)*pageSize},${pageSize}`;
        try {
            let list = await Mysql(sql);
            ctx.state.data = {
                data: {
                    list,
                },
                code: '001'
                // total: total[0].total,
            }
        } catch (error) {
            ctx.state.data = {
                code: '002',
                err: 'مەغلۇپ بولدى'
            }
        }
    } 
    // 播放量排序
    else if (from == 'hot') {
        let sql = `SELECT ${querys} FROM video ORDER BY views DESC limit ${(pageNum-1)*pageSize},${pageSize}`;
        try {
            let list = await Mysql(sql);
            ctx.state.data = {
                data: {
                    list,
                },
                code: '001'
                // total: total[0].total,
            }
        } catch (error) {
            ctx.state.data = {
                code: '002',
                err: 'مەغلۇپ بولدى'
            }
        }
    } 
    // 主页电影列表
    else if (from == 'home') {
        let sql = `SELECT ${querys} FROM video ORDER BY create_time DESC limit 18`;
        let sql2 = `SELECT ${querys} FROM video ORDER BY views DESC limit 18`
        let hotList = await Mysql(sql2);
        let newList = await Mysql(sql);
        ctx.state.data = {
            data: {
                newList,
                hotList
            },
            code: '001'
        }
    }
}